﻿using System;
using System.Security.Principal;
using NHibernate;
using NHibernate.Event;

namespace ContactCenter.AuditEventListener
{
    public abstract class AuditListenerBase
    {
        /// <summary>
        /// Records audit informations.
        /// </summary>
        protected static void PersistAuditInfo(object entity, string propertyName, object oldValue, object newValue, AuditAction action, IEventSource sourceSession)
        {
            ISession session = sourceSession.GetSession(EntityMode.Poco);
            session.Save(new AuditInfo
            {
                Action = action,
                AuditDateTime = DateTime.Now,
                EntityName = entity.GetType().Name,
                FieldName = propertyName,
                OldValue = oldValue.ToString(),
                NewValue = newValue.ToString(),
                Username = WindowsIdentity.GetCurrent().Name
            });
            session.Flush();
        }
    }
}
